﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLT_BLL;
using QLT_Public;

namespace GiaoDien
{
    public partial class uc_NhomNguoiDung : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_NhomNguoiDung()
        {
            InitializeComponent();
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
                ctr.Text = string.Empty;
            foreach (Control i in ctr.Controls)
                Huy_Text(i);
        }
        NhomQuyen_BLL nhomQuyen_bll = new NhomQuyen_BLL();
        private void uc_NhomNguoiDung_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = nhomQuyen_bll.LayDuLieu();
            groupControl1.Hide();
            cobMa.Hide();
            bbi_Huy.Enabled = false;
            bbi_Luu.Enabled = false;

        }
        int flag;
        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            flag = 0;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            bbi_Sua.Enabled = false;
            bbi_Xoa.Enabled = false;
            bbi_Them.Enabled = false;
            groupControl1.Show();
        }

        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            flag = 1;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            bbi_Sua.Enabled = false;
            bbi_Xoa.Enabled = false;
            bbi_Them.Enabled = false;
            gridControl1_DoubleClick(sender,e);
            groupControl1.Show();
        }
        NhomQuyen_Public nq = new NhomQuyen_Public();
        NhatKy_Bll nhatky_bll = new NhatKy_Bll();
        NhatKy_Public nk = new NhatKy_Public();
        Class_TrungGian tg = new Class_TrungGian();
        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (txtTenNhom.Text != "" && cobQuyenHan.Text != "")
            {
                if (flag == 0)
                {

                    //them
                    nq.TenNQ = txtTenNhom.Text;
                    nq.GhiChu = txtGhiChu.Text;
                    nq.MaPQ = int.Parse(cobMa.Text);
                    nhomQuyen_bll.insert_NhomQuyen(nq, nq, nq);
                    //
                    string d = DateTime.Now.ToString();
                    nk.TaiKhoan = tg.TaiKhoan;
                    nk.NgayGio = d;
                    nk.DoiTuong = tg.DoiTuong;
                    nk.HanhDong = "Thêm mới";
                    nk.MoTa = "Thêm mới Nhóm Người Dùng:'" + txtTenNhom.Text + "'";
                    nhatky_bll.insert_NhatKy(nk, nk, nk, nk, nk);
                    //
                    XtraMessageBox.Show("Thêm mới thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Huy_Text(this);
                }
                if (flag == 1)
                {
                    //sua
                    nq.MaNQ = int.Parse(txtMa.Text);
                    nq.TenNQ = txtTenNhom.Text;
                    nq.GhiChu = txtGhiChu.Text;
                    nq.MaPQ = int.Parse(cobMa.Text);
                    nhomQuyen_bll.update_NhomQuyen(nq, nq, nq, nq);
                    //
                    string d = DateTime.Now.ToString();
                    nk.TaiKhoan = tg.TaiKhoan;
                    nk.NgayGio = d;
                    nk.DoiTuong = tg.DoiTuong;
                    nk.HanhDong = "Sửa";
                    nk.MoTa = "Sửa Nhóm Người Dùng:'" + ten + "' thành: '" + txtTenNhom.Text + "'";
                    nhatky_bll.insert_NhatKy(nk, nk, nk, nk, nk);
                    //
                    XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Huy_Text(this);
                }
            }
            else
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin cần thiết!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            uc_NhomNguoiDung_Load(sender,e);
            groupControl1.Show();
        }
        string ten;
        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            groupControl1.Show();
            txtMa.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Nhóm Quyền").ToString();
            ten=txtTenNhom.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Nhóm Quyền").ToString();
            txtGhiChu.Text= gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ghi Chú").ToString();
            cobQuyenHan.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Quyền Hạn").ToString();
        }
        private void bbi_Xoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                nq.MaNQ = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Nhóm Quyền").ToString());
                nq.TenNQ = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Nhóm Quyền").ToString();
                if (XtraMessageBox.Show("Bạn có muốn xóa Nhóm Người Dùng: '" + nq.TenNQ + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    //
                    string d = DateTime.Now.ToString();
                    nk.TaiKhoan = tg.TaiKhoan;
                    nk.NgayGio = d;
                    nk.DoiTuong = tg.DoiTuong;
                    nk.HanhDong = "Xóa";
                    nk.MoTa = "Xóa Nhóm Người Dùng:'" + txtTenNhom.Text + "'";
                    nhatky_bll.insert_NhatKy(nk, nk, nk, nk, nk);
                    //
                    nhomQuyen_bll.delete_NhomQuyen(nq);
                    uc_NhomNguoiDung_Load(sender, e);
                }
            }catch (Exception)
            {
                XtraMessageBox.Show("Không thể xóa do có bản ghi phụ thuộc!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Huy_Text(this);
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
            bbi_Sua.Enabled = true;
            bbi_Xoa.Enabled = true;
            bbi_Them.Enabled =true;
        }

        private void bbi_Thoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            groupControl1.Hide();
        }

        private void bbi_Refresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            uc_NhomNguoiDung_Load(sender,e);
        }

        private void cobQuyenHan_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string sql = "select * from tbl_PhanQuyen";
            dt = nhomQuyen_bll.TaoBang(sql);
            cobMa.DataSource = dt;
            cobMa.DisplayMember = "MaPQ";
            cobQuyenHan.DataSource = dt;
            cobQuyenHan.DisplayMember = "TenQuyen";
        }

        private void cobQuyenHan_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string sql = "select * from tbl_PhanQuyen where TenQuyen=N'"+cobQuyenHan.Text+"'";
            dt = nhomQuyen_bll.TaoBang(sql);
            cobMa.DataSource = dt;
            cobMa.DisplayMember = "MaPQ";
        }
        public void GetMaPhanQuyen(int ma)
        {
            cobMa.Text = ma.ToString();
        }
        private void cobMa_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = "select * from tbl_PhanQuyen where MaPQ='" + cobMa.Text + "'";
                dt = nhomQuyen_bll.TaoBang(sql);
                cobQuyenHan.DataSource = dt;
                cobQuyenHan.DisplayMember = "TenQuyen";
            }
            catch (Exception)
            {
 
            }
        }

        private void txtTenNhom_Leave(object sender, EventArgs e)
        {
            if (txtTenNhom.Text == "")
                txtTenNhom.Focus();
        }

        private void cobQuyenHan_Leave(object sender, EventArgs e)
        {
            if (cobQuyenHan.Text == "")
                cobQuyenHan.Focus();
        }

        private void txtGhiChu_Leave(object sender, EventArgs e)
        {
            //if (txtGhiChu.Text == "")
            //    txtGhiChu.Focus();
        }

        private void txtTenNhom_TextChanged(object sender, EventArgs e)
        {
            string sql = "select * from tbl_NhomQuyen where TenNQ like N'" + txtTenNhom.Text + "'";
            DataTable dt = new DataTable();
            dt = nhomQuyen_bll.TaoBang(sql);
            if (dt.Rows.Count > 0)
            {
                errorProvider1.SetError(txtTenNhom, "Tên Nhóm người dùng đã tồn tại.");
                txtTenNhom.Focus();
            }
            errorProvider1.Clear();
        }



    }
}
